//
//  FriendFrame.m
//  自定义高度
//
//  Created by 逗比小胖！ on 16/2/29.
//  Copyright © 2016年 徐晨. All rights reserved.
//

#import "FriendFrame.h"
#import "Friend.h"

@implementation FriendFrame

- (CGSize)sizeFromString:(NSString *)str font:(UIFont *)font maxWidth:(CGFloat)maxWidth{
    NSDictionary *dic = @{NSFontAttributeName:font};
    CGSize mySize = [str boundingRectWithSize:CGSizeMake(maxWidth, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size;
    
    return mySize;
}

- (void)setFriend:(Friend *)friend{
    _friend = friend;
    CGFloat marginX = 10;
    CGFloat marginY = 10;
    
    CGFloat iconViewX = marginX;
    CGFloat iconViewY = marginY;
    
    self.iconImageViewFrame = CGRectMake(iconViewX, iconViewY, 40, 40);
    
    CGFloat nameLabX = CGRectGetMaxX(self.iconImageViewFrame) + marginX;
    CGFloat nameLabY = iconViewY;
    CGSize nameSize = [self sizeFromString:self.friend.name font:kName maxWidth:MAXFLOAT];
    self.nameLabFrame = (CGRect){{nameLabX,nameLabY},nameSize};
    
    CGFloat textLabX = marginX;
    CGFloat textLabY = CGRectGetMaxY(self.iconImageViewFrame) + marginY;
    CGFloat textLabW = [[UIScreen mainScreen]bounds].size.width - marginX * 2;
    CGSize textSize = [self sizeFromString:self.friend.text font:kText maxWidth:textLabW];
    self.textLabFrame = (CGRect){{textLabX,textLabY},textSize};
    
    CGFloat picViewX = marginX;
    CGFloat picViewY = CGRectGetMaxY(self.textLabFrame) + marginY;
    CGFloat picViewW = [[UIScreen mainScreen]bounds].size.width - marginX * 2;
    CGFloat picViewH = 200;
    
   
    if(self.friend.picture){
        self.picImageViewFrame = CGRectMake(picViewX, picViewY, picViewW, picViewH);
        CGFloat btn1X = marginX;
        NSLog(@"1btn%f",btn1X);
        
        CGFloat btn1Y = CGRectGetMaxY(self.picImageViewFrame) + marginY;
        CGFloat btn1W =([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3;
        self.btn1Frame = CGRectMake(btn1X, btn1Y, btn1W, 30);
        
        CGFloat btn2X = marginX + ([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3;
        NSLog(@"2btn%f",btn2X);
        CGFloat btn2Y = CGRectGetMaxY(self.picImageViewFrame) + marginY;
        CGFloat btn2W =([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3;
        self.btn2Frame = CGRectMake(btn2X, btn2Y, btn2W, 30);
        
        CGFloat btn3X = marginX + ([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3 * 2;
        NSLog(@"3btn%f",btn3X);
        CGFloat btn3Y = CGRectGetMaxY(self.picImageViewFrame) + marginY;
        CGFloat btn3W =([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3;
        self.btn3Frame = CGRectMake(btn3X, btn3Y, btn3W, 30);
        
        self.cellHeight = CGRectGetMaxY(self.btn1Frame) + marginY;
    }else{
        
        CGFloat btn1X = marginX;
        CGFloat btn1Y = CGRectGetMaxY(self.textLabFrame) + marginY;
        CGFloat btn1W =([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3;
        self.btn1Frame = CGRectMake(btn1X, btn1Y, btn1W, 30);
        
        CGFloat btn2X = marginX + ([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3;
        CGFloat btn2Y = CGRectGetMaxY(self.textLabFrame) + marginY;
        CGFloat btn2W =([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3;
        self.btn2Frame = CGRectMake(btn2X, btn2Y, btn2W, 30);
        
        CGFloat btn3X = marginX + ([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3 * 2;
        CGFloat btn3Y = CGRectGetMaxY(self.textLabFrame) + marginY;
        CGFloat btn3W =([[UIScreen mainScreen]bounds].size.width - marginX * 2)/3;
        self.btn3Frame = CGRectMake(btn3X, btn3Y, btn3W, 30);
        
        self.cellHeight = CGRectGetMaxY(self.btn1Frame) + marginY;
    }
    
    
}

@end
